package com.delayqueue.core.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class BeanHelper implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    /**
     * 判断是否实现了接口
     *
     * @param classs
     * @param interfaceClass
     * @param <T>
     * @param <I>
     * @return
     */
    public static <T, I> boolean isImplementFrom(Class<T> classs, Class<I> interfaceClass) {

        Class[] interfaceClasss = classs.getInterfaces();
        if (interfaceClasss.length == 0) {
            return false;
        }
        for (Class aClass : interfaceClasss) {
            if (aClass == interfaceClass) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public <T> T getBean(Class<T> classs) {
        return applicationContext.getBean(classs);
    }
}
